/*
 * Data.cpp
 *
 *  Created on: 19/12/2013
 *      Author: Bruno
 */

#include <stdlib.h>
#include "Tupla.cpp"

struct Modes{

	int Ri;
	int * t;
	int * c;


}

struct Node{

	int cantPred;
	int * P;
}

struct Data{


	int cantAct;
	int T;
	int * S;
	int * R;
	int cantPred;
	Node ** nodes;
	Modes *** modes;
}



Data * crearData(int cantAct, int T, int * &S, int * &R){

	Data* d = new Data();
	d->cantAct = cantAct;
	d->T = T;
	d->R = new int[cantAct];
	d->S = new int[cantAct];

	d->modes = new **Mode[cantAct];

	for(int i = 0; i < cantAct ; i++){
		d->nodes[i] = NULL;
		d->R[i] = R[i];
		d->S[i] = S[i];
		d->modes[i] = new * Mode[R[i]];
		for(int j=0; j < R[i] ; j++){

			d->modes[i][j] = NULL;  //modo j de la actividad i
		}
	}


	return d;
}


Node * crearNode(int cantPred, int * &P){

	Node * g = new Node();
	g->cantPred = cantPred;
	g->P = new int[cantPred];

	for(int i = 0; i < cantPred; i++){

		g->P[i] = P[i];
	}

	return g;

}

Modes * crearModo(int Ri, int* &t, int *&c){

	Modes * m = new Modes();
	m->Ri = Ri;
	m->t = new int[Ri];
	m->c = new int[Ri];

	for(int i = 0; i < Ri; i++){

		m->t[i] = t[i];
		m->c[i] = c[i];
	}

	return m;
}


void agregarNodeData(Data * &d, Node *& g, int act){


	if ( d->nodes[act] != NULL){
		destruirNode(d->nodes[act]);
	}

	d->nodes[act] = crearNode(g->cantPred,g->P);

	return;

}


void agregarModoData(Data * &d, Modes *& m, int act){

	for(int i = 0; i < d->R[act]; i++){

		if(d->modes[act][i] != NULL){ //lugar vacio

			d->modes[act][i] = crearModo(m->Ri,m->t, m->c); //crea una copia limpia
			break;
		}

	}

	return;
}

void destruirNode(Node *&g){

	delete[] g->P;
	delete g;
	g = NULL;
}

void destruirModo(Modes * &m){

	delete[] m->c;
	delete[] m->t;
	delete m;
	m = NULL;

}

void destruirData(Data * &d){

	delete[] d->R;
	delete[] d->S;

	for(int i = 0; i < d->cantAct; i++){
		for(int j = 0; j < d->R[i]; j++){

			destruirModo(d->modes[i][j]);
		}
		delete[] d->modes[i];
		d->modes[i] = NULL;
		destruirNode(d->nodes[i]);
	}

	delete[] d->nodes;
	delete[] d->modes;

	d->modes = NULL;
}

bool esSolucionTupla(int *& tupla, int size){

	for(int i = 0; i < size; i++){
		if (tupla[i] == -1){
			return false;
		}
	}
	return true;
}

//precondicion tupla no es solucion
void crearPaquete(Data* &d, int* &tupla, int sizeT, int &sizeP, int **&p ){

	int pos = getPosTupla(tupla, sizeT);
	sizeP = d->R[pos];
	//TODO aplicar aca la poda!!!

}


